<?php
function alertMes($mes, $url = '')
{
	if ($url != '') {
		exit("<script>alert('{$mes}');window.location='{$url}';</script>");
	} else {
		exit("<script>alert('{$mes}');history.back();</script>");
	}
}
function cachefile($data)
{
	$depr = $data['HOME_URL_PATHINFO_DEPR'];
	//手机版配置文件
	$home_cache_rules = array();
	if ($data['HOME_INDEX_CACHE_ON'] == 1) {
		$home_cache_rules['index:index'] = array("www/{:action}", intval($data['HOME_INDEX_CACHE_TIME']));
	}
	if ($data['HOME_LIST_CACHE_ON'] == 1) {
		$home_cache_rules['list:index'] = array("www/{pathname}/{:action}", intval($data['HOME_LIST_CACHE_TIME']));
		$home_cache_rules['list:lists'] = array("www/{pathname}/{:action}{$depr}{page}", intval($data['HOME_LIST_CACHE_TIME']));
	}
	if ($data['HOME_SHOW_CACHE_ON'] == 1) {
		$home_cache_rules['show:index'] = array("www/{pathname}/{:controller}{$depr}{id}", intval($data['HOME_SHOW_CACHE_TIME']));
	}
	$home = array();
	$home['HTML_CACHE_ON'] = $data['HOME_HTML_CACHE_ON'] == 1 ? true : false;
	$home['HTML_CACHE_TIME'] = 60;
	$home['HTML_FILE_SUFFIX'] = $data['HTML_FILE_SUFFIX'];
	$home['HTML_CACHE_RULES'] = $home_cache_rules;
	$home_text="<?php"."\r\n"."return ".var_export($home,true).";";
	$file = file_put_contents("./Modules/Home/Conf/cache.php",$home_text);
	//生成手机版配置文件
	$mobile_cache_rules = array();
	if ($data['MOBILE_INDEX_CACHE_ON'] == 1) {
		$mobile_cache_rules['index:index'] = array("m/{:action}", intval($data['MOBILE_INDEX_CACHE_TIME']));
	}
	if ($data['MOBILE_LIST_CACHE_ON'] == 1) {
		$mobile_cache_rules['list:index'] = array("m/{pathname}/{:action}", intval($data['MOBILE_LIST_CACHE_TIME']));
		$mobile_cache_rules['list:lists'] = array("m/{pathname}/{:action}{$depr}{page}", intval($data['MOBILE_LIST_CACHE_TIME']));
	}
	if ($data['MOBILE_SHOW_CACHE_ON'] == 1) {
		$mobile_cache_rules['show:index'] = array("m/{pathname}/{:controller}{$depr}{id}", intval($data['MOBILE_SHOW_CACHE_TIME']));
	}
	$mobile = array();
	$mobile['HTML_CACHE_ON'] = $data['MOBILE_HTML_CACHE_ON'] == 1 ? true : false;
	$mobile['HTML_CACHE_TIME'] = 60;
	$mobile['HTML_FILE_SUFFIX'] = $data['HTML_FILE_SUFFIX'];
	$mobile['HTML_CACHE_RULES'] = $mobile_cache_rules;
	$mobile_text="<?php"."\r\n"."return ".var_export($mobile,true).";";
	$file = file_put_contents("./Modules/Mobile/Conf/cache.php",$mobile_text);

}


/**
 * 返回保存到attachmentindex表
 * @param string $name 元素名称
 * @param integer $typeid 类型
 * @param string $tvalue 表单类型和可选值
 * @param string|integer $vaule 值
 * @return mixed
 */

function get_element_html($name,$typeid, $tvalue = '', $vaule = '') {
	if (empty($name) || empty($typeid)) {
		return '';
	}
	switch ($typeid) {
		case 1:
			$type = 'text';
			$vaule = intval($vaule);
			break;
		case 2:
			$type = 'text';
			break;
		case 3:
			$type = 'textarea';
			break;
		case 4:
			$type = 'radio';			
			$vaule = intval($vaule);
			break;		
		default:
			$type = 'text';
		break;
	}
	if (!empty($tvalue)) {
		$array = explode("\n", str_replace("\r\n", "\n", trim($tvalue,"\r\n")));
		if (in_array($array[0], array('select','radio','checkbox','text','textarea'))) {
			$type = $array[0]; 
			unset($array[0]);
			if(strpos($tvalue,'|')){
				$tvalue  = array();
				foreach ($array as $val) {
				    list($k, $v) = explode('|', $val);
				    $tvalue[$k]   = $v;
				}
			}else{
				foreach ($array as $val) {
				    $tvalue[$val]   = $val;
				}
			}
		}	
	}
	$str = '';
	switch ($type) {
		case 'text':
			$str = '<input type="text"  class="layui-input" name="'.$name.'" value="'.$vaule.'">';
			break;
		case 'textarea':
			$str = '<textarea name="'.$name.'" id="'.$name.'" class="layui-textarea">'.$vaule.'</textarea>';
			break;
		case 'radio':
			if (!is_array($tvalue)) {
				$tvalue = array(1=>'是',0=>'否');
			}
			foreach ($tvalue as $k => $v) {
				$str .= '<input type="radio" value="'.$k.'" title="'.$v.'"  name="'.$name.'"  ';
				if ($vaule == $k) {
					$str .= 'checked="checked" ';
				}
				$str .=	'/>';
			}
			break;		
		case 'select':
			$str .= '<select name="'.$name.'" lay-verify="required">';
			$str .= '<option value="default" ';
			$str .=	'>'.'default'.'</option>';
			$str .= '</select>';
			break;
		default:
			$str = '';
			break;
	}
	return $str;
}
/**
 * 获取文件目录列表
 * @param string $pathname 路径
 * @param integer $fileFlag 文件列表 0所有文件列表,1只读文件夹,2是只读文件(不包含文件夹)
 * @param string $pathname 路径
 * @return array
 */
function get_file_folder_List($pathname, $fileFlag = 0, $pattern = '*')
{
    $fileArray = array();
    $pathname  = rtrim($pathname, '/') . '/';
    $list      = glob($pathname . $pattern);
    foreach ($list as $i => $file) {
        switch ($fileFlag) {
            case 0:
                $fileArray[] = basename($file);
                break;
            case 1:
                if (is_dir($file)) {
                    $fileArray[] = basename($file);
                }
                break;
            case 2:
                if (is_file($file)) {
                    $fileArray[] = basename($file);
                }
                break;
            default:
            break;
        }
    }
    if (empty($fileArray)) {
        $fileArray = null;
    }
    return $fileArray;
}
/**
 * 获取对应组的联动列表
 * @param string $group 联动组名
 * @param integer $value 联动值
 * @return array
 */
function get_item($group = 'animal', $update = 0)
{

    //S方法的缓存名都带's'
    $itme_arr = S('sItem_' . $group);
    if ($update || !$itme_arr) {
        $itme_arr = array();
        $temp     = M('configinfo')->where(array('group' => $group))->order('sort,id')->select();
        foreach ($temp as $key => $v) {
            $itme_arr[$v['value']] = $v['name'];

        }
        //S(缓存名称,缓存值,缓存有效时间[秒]);
        S('sItem_' . $group, $itme_arr, 48 * 60 * 60);
    }
    return $itme_arr;
}
/**
 * 获取联动(字典)项的值
 * @param string $group 联动组名
 * @param integer $value 联动值
 * @return string
 */
function get_item_value($group, $value = 0)
{
    ${'item_' . $group} = get_item($group);
    if (isset(${'item_' . $group}[$value])) {
        return ${'item_' . $group}[$value];
    } else {
        return "保密";
    }
}
function sendEmail($info,$data,$content,$title)
{
	require './Public/swiftmailer/lib/swift_required.php';
	//发送邮件，QQ邮箱
	//配置邮件服务器，得到传输对象
	$transport = Swift_SmtpTransport::newInstance($data['CFG_EMAIL_HOST'],$data['CFG_EMAIL_PORT'],'ssl');
	//设置登录账号和密码
	$transport->setUsername($data['CFG_EMAIL_LOGINNAME']);
	$transport->setPassword($data['CFG_EMAIL_PASSWORD']);
	//得到发送邮件对象Swift_Mailer对象
	$mailer = Swift_Mailer::newInstance($transport);
	//得到邮件对象
	$message = Swift_Message::newInstance();
	//设置管理员的信息
	$arr[$data['CFG_EMAIL_LOGINNAME']] = $data['CFG_EMAIL_FROM_NAME'];
	$message->setFrom($arr);
	//邮件发给谁
	$brr[$info['email']]=$info['name'];
	$message->setTo($brr);
	//设置邮件主题
	$message->setSubject("{$title}");
	$message->setBody("{$content}",'text/html','utf-8');
	try {
		if ($mailer->send($message)) {
			return true;
		} else {
			return false;
		}
	} catch (Swift_ConnectionException $e) {
		alertMes("邮件发送错误" . $e->getMessage());
	}
}


